package cn.wangzg.viewpagerdemo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragments=new ArrayList<>();
        fragments.add(OneFragment.newInstance(R.color.purple_200,"One"));
        fragments.add(OneFragment.newInstance(R.color.purple_500,"Two"));
        fragments.add(OneFragment.newInstance(R.color.purple_200,"Three"));
        fragments.add(OneFragment.newInstance(R.color.purple_500,"Four"));

        ViewPager viewPager=findViewById(R.id.vp);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(),1) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return "项目"+position;
            }
        });
        viewPager.setPageTransformer(false,
                new RotatePageTransformer());
        TabLayout tabLayout=findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
    }
    class RotatePageTransformer implements ViewPager.PageTransformer {
        private static final float MAX_ROTATION = 20.0f;
        /**
         * position取值特点：
         * 假设页面从0～1，则：
         * 第一个页面position变化为[0,-1]
         * 第二个页面position变化为[1,0]
         *
         * @param page
         * @param position
         */
        @Override
        public void transformPage(View page, float position) {
            System.out.println("-----position:" + position);
            if (position < -1) {
                //rotate(page, -MAX_ROTATION);
            } else if (position <= 1) {
                rotate(page, MAX_ROTATION * position);
            } else {
                //rotate(page, MAX_ROTATION);
            }
        }

        private void rotate(View view, float rotation) {
            view.setPivotX(view.getWidth() * 0.5f);
            view.setPivotY(view.getHeight());
            view.setRotation(rotation);
        }
    }

    public void login(String name,String pwd){
        //1.Url
        try {
            URL url=new URL("https://www.wanandroid.com/user/login");
            //2.HttpUrlConnection
            HttpURLConnection cn= (HttpURLConnection) url.openConnection();
            //3.设置参数
            cn.setRequestMethod("POST");
            cn.setDoOutput(true);
            cn.setUseCaches(false);
            //4.流将参数写到服务器
            String para="username="+ URLEncoder.encode(name,"utf-8")+
                    "&password="+URLEncoder.encode(pwd,"utf-8");
            OutputStream outputStream = cn.getOutputStream();
            outputStream.write(para.getBytes());
            outputStream.flush();
            //5.获得响应的输入流
            if(cn.getResponseCode()==200){
                //6.获得字符串
                InputStream inputStream = cn.getInputStream();

                //7、解析
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}